package Learning;

import  java.sql.*;

public class demo2 {
    /*
    完成数据的插入与删除
     */
    public static void main(String[] args) {
        Connection con = null;
        Statement ste = null;
        try{
            // 获取驱动
             DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            // 获取连接
            String url = "jdbc:mysql://192.168.10.132:3306/learn_JDBC?useUnicode=true&characterEncoding=utf-8";
            con = DriverManager.getConnection(url,"root", "123456");
            // 获取数据库操作对象
            ste = con.createStatement();
            // 执行数据库语句
//            String sql = "delete from user where u_gender = '男'";
            String sql = "update user set u_name ='她' where u_gender = '女'";
            // 返回值是“影响的数据库中的记录条数”
            int count = ste.executeUpdate(sql);
            System.out.println(count != 0 ? "保存成功,影响了"+count+"条数据" : "失败");
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            try {
                if (con != null){
                    con.close();
                    }
                }catch (SQLException e){
                e.printStackTrace();
                }
            try {
                if (ste != null){
                    ste.close();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
}
